package com.taoding.hfhclient.rabbitmq;

import com.rabbitmq.client.Channel;
import com.taoding.hfhclient.HttpClient.HttpRequestTest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author liuxinghong
 * @Description:
 * @date 2019/8/28 002817:45
 */
@Component
@Slf4j
public class RabbitMQListener {

    @Autowired
    private RabbitMQConfiguration rabbitMQConfiguration;
    @Autowired
    private HttpRequestTest httpRequestTest;

    @Bean
    public MessageListener exampleListener() {
        return new ChannelAwareMessageListener() {
            @Override
            public void onMessage(Message message, Channel channel) {
                try {

                    log.info("接收到的消息数据》》》："+ new String(message.getBody(), StandardCharsets.UTF_8));
                    //todo：调用业务方法
                    httpRequestTest.printData(new String(message.getBody(), StandardCharsets.UTF_8));
                    channel.basicAck(message.getMessageProperties().getDeliveryTag(), true); //确认消息已经消费
                }catch (Exception e){
                    log.error(e.getMessage());
                    try {
                        channel.basicNack(message.getMessageProperties().getDeliveryTag(), true, true); //确认消息未消费，可再次消费
                    } catch (IOException e1) {
                        log.error(e.getMessage());
                        e.printStackTrace();
                    }
                }
//                finally {
//                    //最终确认消息已消费（慎用次操作，操作后消息将不复存在）
//                    try {
////                        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
//                    } catch (IOException e) {
//                        log.error(e.getMessage());
//                        e.printStackTrace();
//                    }
//
                }
        };
    }

    @Bean
    public SimpleMessageListenerContainer messageListenerContainer() {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(rabbitConnectionFactory());
        //设置监听的队列名，数组[]"abc","test4"
        String[] types = {rabbitMQConfiguration.getReceiverQueueName()};
        container.setQueueNames(types);
        container.setMessageListener(exampleListener());
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);//手动确认
        return container;
    }

    @Bean
    public ConnectionFactory rabbitConnectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(rabbitMQConfiguration.getHost());
        connectionFactory.setUsername(rabbitMQConfiguration.getUserName());
        connectionFactory.setPassword(rabbitMQConfiguration.getPassword());
        connectionFactory.setPort(rabbitMQConfiguration.getPort());
        connectionFactory.setVirtualHost(rabbitMQConfiguration.getVirtualHost());
        return connectionFactory;
    }



}
